import store from '@/store'
// import { config } from '@vue/test-utils'
import axios from 'axios'
import { Message } from 'element-ui'

// 创建axios请求实例

const service = axios.create({
  baseURL: '/tj', // 和代理一样
  timeout: 20000 // 超时等待
})

// 请求拦截器
service.interceptors.request.use(config => {
//  注入token
  if (store.getters.token) {
    config.headers.Authorization = `Bearer ${store.getters.token}`
  }
  return config // 必须进行返回 必须放在最外层
}, error => Promise.reject(error))

// 响应拦截器
service.interceptors.response.use((response) => {
  const { data, code, msg } = response.data
  if (code) {
    return data // 返回有意义的数据
  }
  Message.error(msg)
  return Promise.reject(new Error(msg))
}, error => {
  Message.error(error.msg)
  return Promise.reject(error)
})

export default service
